package JDBCUtils;

import org.junit.Test;

import java.sql.*;


public class JDBCUtils {
    private static ResultSet rs;
    private Connection conn;

    //    获取连接
    public static Connection getConnection() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/study2", "root", "root");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return conn;
    }

    //    释放连接
    public static void releaseSource(Connection conn, PreparedStatement ps, ResultSet rs) {
        try {
            if (ps != null) {
                ps.close();
            }
            if (conn != null) {
                conn.close();
            }
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


    }

    public static void releaseSource(Statement st, Connection conn) {
        try {
            if (st != null) {
                st.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}
//    @Test

//    public void queryTest() throws SQLException {
////3.通过工具类获得连接
//        conn = JDBCUtils.getConnection();
////        发送SQL语句到数据库对象
//
//// 4 发送sql语句，存储结果集
//        String sql = "select * from users where  username =? and password =?";
//        PreparedStatement ps = conn.prepareStatement(sql);
//        ps.setString(1,username);
//        ResultSet rs = ps.executeQuery();
//// 5 解析得到结果集
//        while (rs.next()) {
//
//            String name = rs.getString("name");
//            String password = rs.
//            System.out.println(id + "--" + name + "--" + age + "---" + job + "---" + sal);
//        }
////通过工具类释放连接
//        JDBCUtils.releaseSource(ps, st, conn);
//    }
//}
